package com.rybbaby.tss.platform.listener;

import com.rybbaby.tss.core.utils.SpringApplicationContextUtil;
import com.rybbaby.tss.platform.consts.PlatformCommonConsts;
import com.rybbaby.tss.platform.sysAdminUser.entity.SysAdminUser;
import com.rybbaby.tss.platform.sysAdminUser.service.ISysAdminUserService;
import com.rybbaby.tss.platform.websocket.service.PlatformWebsocketMsgService;
import org.apache.log4j.Logger;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;

/**
 * @author 谢进伟
 * @description Session 监听
 * @createDate 2017年1月5日 上午11:09:35
 */
public class SessionLinster implements HttpSessionListener {

    private static ISysAdminUserService sysAdminUserService;
    private Logger log = Logger.getLogger(SessionLinster.class);

    /**
     * webSocket消息服务
     */
    private static PlatformWebsocketMsgService platformWebsocketMsgService;

    @Override
    public void sessionCreated(HttpSessionEvent hse) {
        String sessionId = hse.getSession().getId();
        log.info("session:" + sessionId + "创建");
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent hse) {
        HttpSession session = hse.getSession();
        String sessionId = session.getId();
        Object loginAdminUserId = session.getAttribute(PlatformCommonConsts.SESSION_LOGINUSER_OBJ_ID);
        if (sysAdminUserService == null) {
            Object bean = SpringApplicationContextUtil.getBean(ISysAdminUserService.class);
            if (bean != null && bean instanceof ISysAdminUserService) {
                sysAdminUserService = (ISysAdminUserService) bean;
            }
        }
        if (platformWebsocketMsgService == null) {
            Object bean = SpringApplicationContextUtil.getBean(PlatformWebsocketMsgService.class);
            if (bean != null && bean instanceof PlatformWebsocketMsgService) {
                platformWebsocketMsgService = (PlatformWebsocketMsgService) bean;
            }
        }
        if (sysAdminUserService != null) {
            List<SysAdminUser> onlinLoginUser = sysAdminUserService.findByProperty("onlineSessionId", sessionId);
            // 登录用户自动下线
            for (SysAdminUser loginUser : onlinLoginUser) {
                log.info(new Timestamp(System.currentTimeMillis()) + "用户：" + loginUser.getUserName() + " 即将自动离线...");
                loginUser.setIsOnline(0);
                loginUser.setOnlineSessionId(null);
                sysAdminUserService.updateEntitiy(loginUser);
                log.info(new Timestamp(System.currentTimeMillis()) + "用户：" + loginUser.getUserName() + "自动离线成功!");
            }
        }
        try {
            //通知客户端登陆超时
            platformWebsocketMsgService.sendMessageToUser("-1", "系统", sessionId + "-" + loginAdminUserId, "SystemLoginTimeoutNotify");
        } catch (IOException e) {
            e.printStackTrace();
        }
        log.info("session:" + sessionId + "销毁");
    }
}
